package com.telit.firecommand.util;

import com.telit.common.util.OssUtils;
import com.telit.common.vo.FileVO;
import org.apache.http.entity.ContentType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @Author:zhangwei
 * @Description:
 * @Time:Created on 2022/4/6 14:51
 */
public class FireImgUtil {

    /**
     * 通过火情图片地址下载图片到本地
     */
    public static File downloadFile(String remoteFilePath, String localFilePath, String fileName) {
        URL urlfile = null;
        HttpURLConnection httpUrl = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {

            File dir = new File(localFilePath);
            if (!dir.exists()) {//判断文件目录是否存在
                dir.mkdirs();
            }

            File f = new File(localFilePath+fileName);

            urlfile = new URL(remoteFilePath);
            httpUrl = (HttpURLConnection) urlfile.openConnection();
            httpUrl.connect();
            bis = new BufferedInputStream(httpUrl.getInputStream());
            bos = new BufferedOutputStream(new FileOutputStream(f));
            int len = 2048;
            byte[] b = new byte[len];
            while ((len = bis.read(b)) != -1) {
                bos.write(b, 0, len);
            }
            System.out.println("上传成功");
            bos.flush();
            bis.close();
            httpUrl.disconnect();
            return f;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                bis.close();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 将图片存储到本地服务器并返回图片地址
     */
    public static String getFilePath(File file) throws IOException {
        MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),
                ContentType.APPLICATION_OCTET_STREAM.toString(), new FileInputStream(file));
//        String firePath = MinioUtils.upload(multipartFile, MinioConstants.BUCKET_FORESTRY_NAME, MinioConstants.BUCKET_FIRECOMMAND_SUFFIX_NAME);
        FileVO aPublic = OssUtils.upload(multipartFile, "public", null);
        return aPublic.getFilePath();
    }

    public static void main(String[] args) {
//        FireImgUtil util=new FireImgUtil();
//        String remoteFilePath="http://192.168.120.121:65514/bforestfire/v1/picture/preview?picUrl=9d29=9a16ia6-=o6418p79d7f13f-ba3413db4*495==sp**016==t6*6114397545l9*1915=7o4*962-=2*3eb9od19bl49-941207";
//        String localFilePath="D:/img/";
//        String fileName="1234.jpg";
//        util.downloadFile(remoteFilePath,localFilePath,fileName);

        String imageUrl="/pic?9d29=9a16ia6-=o6418p79d7f13f-ba3413db4*495==sp**016==t6*6114397545l9*1915=7o4*962-=2*3eb9od19bl49-941207";
        /*"imageUrl":"/pic?9d29=9a16ia6-=o6418p79d7f13f-ba3413db4*495==sp**016==t6*6114397545l9*1915=7o4*962-=2*3eb9od19bl49-941207"*/
        /*https://36.34.102.90:65514/bforestfire/v1/picture/preview?picUrl=9d29=9a16ia6-=o6418p79d7f13f-ba3413db4*495==sp**016==t6*6114397545l9*1915=7o4*962-=2*3eb9od19bl49-941207*/
        //按照规则截取指定的字符串
        imageUrl = imageUrl.substring(imageUrl.indexOf("/pic?")+5);
        FireImgUtil util=new FireImgUtil();
        String remoteFilePath="http://192.168.120.121:65514/bforestfire/v1/picture/preview?picUrl="+imageUrl;
        String localFilePath="D:/img/";
        String fileName="3452.jpg";
        util.downloadFile(remoteFilePath,localFilePath,fileName);
        System.out.println(imageUrl);
    }

}
